#include "example_month_hall.h"

void init() {
  srand(time(NULL));
  return;
}

// [min,max)
int random_int(int min, int max) { return min + rand() % (max - min); }

// 这里是为了复制而复杂的例子
MResult monty_hall_simulation(int num) {
  MResult res = {0, 0};
  int win_by_switching = 0;
  int win_by_sticking = 0;
  for (int i = 0; i < num; i++) {
    int car_position = random_int(0, 3);
    int contestant_choice = random_int(0, 3);
    int doors[2] = {0, 0};
    int d_i = 0;
    for (int j = 0; j < 3; j++) {
      if (j != car_position && j != contestant_choice) {
        doors[d_i++] = j;
      }
    }
    int host_open = doors[random_int(0, d_i)];
    if (contestant_choice == car_position) {
      win_by_sticking++;
    } else {
      int switch_choice;
      for (int j = 0; j < 3; j++) {
        if (j != contestant_choice && j != host_open) {
          switch_choice = j;
          break;
        }
      }
      if (switch_choice == car_position) {
        win_by_switching++;
      }
    }
  }
  res.win_by_switching = win_by_switching;
  res.win_by_sticking = win_by_sticking;
  return res;
}
